package com.meitutest.autoscript.fluency;

import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;


/**
 * Created by CrystalChen on 2017/9/25.
 */

public class SwipeDevice {
    private UiDevice uiDevice;
    private int h;
    private int w;

    private int h_x1;
    private int h_y1;
    private int h_x2;
    private int h_y2;

    private int v_x1;
    private int v_y1;
    private int v_x2;
    private int v_y2;


    public SwipeDevice(UiDevice uiDevice) {
        this.uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        this.h = uiDevice.getDisplayHeight();
        this.w = uiDevice.getDisplayWidth();

        this.h_x1 = (int) (w * 0.2);
        this.h_y1 = (int) (h * 0.5);
        this.h_x2 = (int) (w * 0.8);
        this.h_y2 = (int) (h * 0.5);

        this.v_x1 = (int) (w * 0.5);
        this.v_y1 = (int) (h * 0.2);
        this.v_x2 = (int) (w * 0.5);
        this.v_y2 = (int) (h * 0.8);
    }

    public void fromLeft(int counts) {
        swipe(counts, h_x1, h_y1, h_x2, h_y2);

    }

    public void fromRight(int counts) {
        swipe(counts, h_x2, h_y2, h_x1, h_y1);
    }

    public void fromRight(int counts, int step) {
        swipe(counts, h_x2, h_y2, h_x1, h_y1, step);
    }

    public void fromBottom(int counts) {
        swipe(counts, v_x2, v_y2, v_x1, v_y1);
    }

    public void fromBottom(int counts, int step) {
        swipe(counts, v_x2, v_y2, v_x1, v_y1, step);
    }

    public void fromTop(int counts) {
        swipe(counts, v_x1, v_y1, v_x2, v_y2);
    }


    void swipe(int count, int x1, int y1, int x2, int y2) {
        for (int i = 0; i < count; i++) {
            check();
            uiDevice.swipe(x1, y1, x2, y2, CommonArgs.step);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    void swipe(int count, int x1, int y1, int x2, int y2, int step) {
        for (int i = 0; i < count; i++) {
            uiDevice.swipe(x1, y1, x2, y2, step);
//            try {
//                Thread.sleep(10000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
    }

    private void check() {
        try {
            if (uiDevice.findObject(new UiSelector().text("同意")).exists()) {
                uiDevice.findObject(By.text("同意")).click();
            } else if (uiDevice.findObject(new UiSelector().text("允许")).exists()) {
                uiDevice.findObject(By.text("允许")).click();
            } else if (uiDevice.findObject(new UiSelector().text("给我们评分")).exists()) {
                uiDevice.findObject(By.text("不了，谢谢")).click();
            } else if (uiDevice.findObject(new UiSelector().textContains("更新")).exists()) {
                uiDevice.findObject(By.text("以后再说")).click();
            } else if (uiDevice.findObject(new UiSelector().resourceId("com.jingdong.app.mall:id/jw")).exists()) {
                uiDevice.findObject(new UiSelector().resourceId("com.jingdong.app.mall:id/jw")).click();
            } else if (uiDevice.findObject(new UiSelector().textContains("提示")).exists()) {
                uiDevice.findObject(By.text("确定")).click();
            } else if (uiDevice.findObject(new UiSelector().resourceId("com.taobao.taobao:id/homepage_overlay_close")).exists()) {
                uiDevice.findObject(new UiSelector().resourceId("com.taobao.taobao:id/homepage_overlay_close")).click();
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

}
